package srv

import (
	"arc_srv4/common"
	"arc_srv4/logutils"
	"github.com/kataras/iris/v12"
	"go.uber.org/zap"
)

func DownLoadAttachment(ctx iris.Context) {
	path := common.AppConCfg.Attachment + common.ATTACH_DOWNLOAD
	print(path)
	ib := makeDir(path)
	if !ib {
		logutils.Logger.Error("download file path is not exists", zap.String("path", path))
		_, _ = ctx.JSON(common.ApiRspOnly{Code: 500, Message: "下载失败"})
		return
	}
	fileName := ctx.Params().Get("name")
	if fileName == "" {
		logutils.Logger.Error("download file is not exists", zap.String("path", path))
		_, _ = ctx.JSON(common.ApiRspOnly{Code: 500, Message: "文件不存在"})
		return
	}
	file := path + "/" + fileName
	err := ctx.SendFile(file, fileName)

	if err != nil {
		logutils.Logger.Error("download error", zap.String("error", err.Error()))
		_, _ = ctx.JSON(common.ApiRspOnly{Code: 500, Message: "下载失败"})
		return
	}
	logutils.Logger.Info("download success")
	_, _ = ctx.JSON(common.ApiRspOnly{Code: 200, Message: "下载成功"})
}

func DownLoadElectron(ctx iris.Context) {
	path := common.AppConCfg.Attachment + common.ATTACH_DOWNLOAD
	print(path)
	ib := makeDir(path)
	if !ib {
		logutils.Logger.Error("download file path is not exists", zap.String("path", path))
		_, _ = ctx.JSON(common.ApiRspOnly{Code: 500, Message: "下载失败"})
		return
	}
	fileName := ctx.Params().Get("name")
	if fileName == "" {
		logutils.Logger.Error("download file is not exists", zap.String("path", path))
		_, _ = ctx.JSON(common.ApiRspOnly{Code: 500, Message: "文件不存在"})
		return
	}
	file := path + "/" + fileName
	err := ctx.SendFile(file, fileName)

	if err != nil {
		logutils.Logger.Error("download error", zap.String("error", err.Error()))
		_, _ = ctx.JSON(common.ApiRspOnly{Code: 500, Message: "下载失败"})
		return
	}
	logutils.Logger.Info("download success")
	_, _ = ctx.JSON(common.ApiRspOnly{Code: 200, Message: "下载成功"})
}
